package dat255.calendarclock.controller.event.activity;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import dat255.calendarclock.R;
import dat255.calendarclock.model.event.Category;
import dat255.calendarclock.model.event.EventHandler;
import dat255.calendarclock.view.ColorVisualizer;

/**
 * The activity where a customized event
 * @author Gabriel Andersson
 */
public class CreateCategoryActivity extends Activity {

	// Seekbars
	private static SeekBar seekBarR;
	private static SeekBar seekBarG;
	private static SeekBar seekBarB;
	private static ColorVisualizer colorVisializer;
	//Listeners
	private static OnSeekBarChangeListener listenerR = new OnSeekBarChangeListener() {
		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
		}

		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			colorR = progress;
			colorVisializer.setColor(Color.rgb(colorR, colorG, colorB));
			colorVisializer.invalidate();
		}
	};
	private static OnSeekBarChangeListener listenerG = new OnSeekBarChangeListener() {
		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
		}

		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			colorG = progress;
			colorVisializer.setColor(Color.rgb(colorR, colorG, colorB));
			colorVisializer.invalidate();
		}
	};
	private static OnSeekBarChangeListener listenerB = new OnSeekBarChangeListener() {
		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
		}

		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			colorB = progress;
			colorVisializer.setColor(Color.rgb(colorR, colorG, colorB));
			colorVisializer.invalidate();
		}
	};
	

	// Color values to create color
	private static int colorR = 9;
	private static int colorG = 205;
	private static int colorB = 218;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.createcategory_layout);
		colorVisializer = (ColorVisualizer) findViewById(R.id.createCategory_colorVisualizer);
		seekBarR = (SeekBar) findViewById(R.id.createCategory_seekBarR);
		seekBarR.setOnSeekBarChangeListener(listenerR);
		seekBarG = (SeekBar) findViewById(R.id.createCategory_seekBarG);
		seekBarG.setOnSeekBarChangeListener(listenerG);
		seekBarB = (SeekBar) findViewById(R.id.createCategory_seekBarB);
		seekBarB.setOnSeekBarChangeListener(listenerB);

	}

	@Override
	protected void onResume() {
		super.onResume();
		seekBarR.setProgress(9);
		seekBarG.setProgress(205);
		seekBarB.setProgress(218);
	}

	/**
	 * The customized Category is "added"
	 */
	public void doneButtonClicked(View view) {
		EditText editText = (EditText) findViewById(R.id.createCategory_name);
		String name = editText.getEditableText().toString();
		EventHandler.getInstance().setCustomCategory(
				name.equals("") ? null : Category.createCategory(name,
						Color.rgb(colorR, colorG, colorB)));
		this.finish();
	}

}
